package com.pac.trigger;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.common.tools.TimeTools;
import com.common.util.PropertyUtils;
import com.kuanrf.commons.weixin.api.WxMpTemplateService;
import com.kuanrf.commons.weixin.msg.bean.WxMpTemplateBase;
import com.kuanrf.commons.weixin.msg.bean.WxMpTemplateMessage;
import com.pac.weixin.WeiXinKeyContent;
import com.pac.weixin.bean.WxChatAutoBean;
import com.pac.weixin.bean.WxMpTemplateFollow;
import com.pac.weixin.service.WxChatAutoService;
import com.pac.weixin.service.WxChatService;

/**
 * 定时任务
 * 
 * @author lujun.chen
 * @version [版本号, 2015年3月13日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Component(value = "trigger")
public class Trigger {

	private static Logger logger = Logger.getLogger(Trigger.class);

	@Autowired
	private WxChatService wxChatService;

	@Autowired
	private WxChatAutoService wxChatAutoService;

	@Autowired
	private WxMpTemplateService wxMpTemplateService;

	/**
	 * 定时发送微信消息
	 * 
	 * @see [类、类#方法、类#成员]
	 */
	public void autoSendWxChat() {

		WxChatAutoBean wxChatAutoBean = new WxChatAutoBean();
		wxChatAutoBean.setState(WeiXinKeyContent.WEIXIN_CHAT_STATE_READ_NO);
		wxChatAutoBean.setSendDateEnd(TimeTools.now());

		// 微信消息模板ID
		String wxTemplateIdFollow = PropertyUtils
				.getValue("weixin.template.id.follow");

		List<WxChatAutoBean> wxChatAutoList = wxChatAutoService
				.queryWxChatAuto(wxChatAutoBean);
		for (WxChatAutoBean chatAuto : wxChatAutoList) {
			try {

				String url = chatAuto.getTemplateUrl();
				WxMpTemplateMessage message = new WxMpTemplateMessage();
				message.setTemplate_id(wxTemplateIdFollow);
				message.setTouser(chatAuto.getOpenId());
				message.setUrl(url);
				message.setTopcolor("#FF0000");

				WxMpTemplateFollow followUp = new WxMpTemplateFollow();
				
				//头信息
				WxMpTemplateBase first=new WxMpTemplateBase();
				first.setValue(chatAuto.getPatient()+"，您好，您的诊后随访如下");
				first.setColor("#173177");
				followUp.setFirst(first);
				
				// 随访科室
				WxMpTemplateBase keyword1 = new WxMpTemplateBase();
				keyword1.setValue("妇产科");
				keyword1.setColor("#173177");
				followUp.setKeyword1(keyword1);

				// 随访时间
				WxMpTemplateBase keyword2 = new WxMpTemplateBase();
				keyword2.setValue(TimeTools.now("yyyy年MM月dd日"));
				keyword2.setColor("#173177");
				followUp.setKeyword2(keyword2);

				// 活动内容
				WxMpTemplateBase keyword3 = new WxMpTemplateBase();
				keyword3.setValue(chatAuto.getTemplateTitle());
				keyword3.setColor("#173177");
				followUp.setKeyword3(keyword3);
				
				//备注
				WxMpTemplateBase remark=new WxMpTemplateBase();
				remark.setValue(chatAuto.getTemplateContent());
				remark.setColor("#FF4500");
				followUp.setRemark(remark);

				message.setData(followUp);
				wxMpTemplateService.send(message);

				
			} catch (Exception ex) {
				logger.error("微信自动发送内容出错，" + ex);
			}
			finally
			{
				String autoChatId = chatAuto.getId();
				WxChatAutoBean wxChatAutoU = new WxChatAutoBean();
				wxChatAutoU
						.setState(WeiXinKeyContent.WEIXIN_CHAT_STATE_READ_YES);
				wxChatAutoU.setId(autoChatId);
				wxChatAutoService.updateWxChatAuto(wxChatAutoU);
			}
		}

	}

}
